package cn.edu.ctbu.datajpa01.service;

import cn.edu.ctbu.datajpa01.domain.Teacher;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import cn.edu.ctbu.datajpa01.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    private TeacherRepository TeacherRepository;

    public List<Teacher> findAll() {
        return TeacherRepository.findAll();
    }

    /**
     * 按id进行查询
     * @param id  主键，整数
     * @return
     */
    public Teacher findById(Integer id) {
        return TeacherRepository.findById(id).orElse(null);
    }
    /**
     * 按 工号no 进行查询
     * @param no  工号
     * @return
     */
    public Teacher findByNo(String no) {
        return TeacherRepository.findByNo(no);
    }
    /**
     * 按姓名进行查询  like
     * @param name  姓名
     * @return
     */
    public List<Teacher> findByName(String name) {
        return TeacherRepository.findByNameLike(name);
    }
    /**
     * 插入即新增
     * @param teacher
     */
    public void insert(Teacher teacher) {
        TeacherRepository.save(teacher);
    }
    /**
     * 更新即更改
     * @param teacher
     */
    public void update(Teacher teacher) {
        TeacherRepository.save(teacher);
    }
    /**
     * 删除
     * @param teacher
     */
    public void delete(Integer id) {
        TeacherRepository.deleteById(id);
    }
    /**
     * 按工号进行查询  ，查找工号最大的教师
     */
    // 修改方法调用为findByNoStartingWith，并传入前四位工号
    public List<Teacher> findByMaxNoTeacher() {
        // 获取前四位工号
        String maxNoPrefix = TeacherRepository.findMaxNoPrefix();
        // 查找以该前缀开头的教师
        return TeacherRepository.findByNoStartingWith(maxNoPrefix);
    }
}
